# Definition for singly-linked list.
from sys import prefix
from typing import Optional, List


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def nextLargerNodes(self, head: Optional[ListNode]) -> List[int]:
        nums = []
        while head:
            nums.append(head.val)
            head = head.next

        stack = []
        ret = [-1] * len(nums)
        for idx in range(0, len(nums)):
            while stack and nums[idx] > nums[stack[-1]]:
                ret[stack.pop()] = idx
            stack.append(idx)
        # print(ret)

        vals = [0] * len(nums)
        for idx in range(0, len(nums)):
            if ret[idx] != -1:
                vals[idx] = nums[ret[idx]]
        return vals
